$('.select2').select2()
getData(1);
$('.search_btn1').click(function () {
    getData(1);
})

function getData(curr) {
    var group1 = $("#kind_sel1").select2('val');
    var group2 = $("#kind_sel2").select2('val');
    // var kind_text = $(".kind_sel").select2('val');
    $.ajax({
        type: "get",
        url: "http://www.puer.zone:30000/FindProductType",
        async: true,
        data: {
            ProductGroup1: group1,
            ProductGroup2: group2,
            page: curr,
            row: 10
        },
        success: function (data) {
            if (data[0].length == 0) {
                layer.msg('暂无数据');
            }
            pageDivd(curr, data[1]);
            TableView(data[0]);
            $('.all_data').text(data[1])
        }
    });
}

function pageDivd(curr, page_num) {
    layui.use(['laypage', 'layer'], function () {
        var laypage = layui.laypage
            , layer = layui.layer;
        laypage.render({
            elem: 'demo7'
            , count: page_num
            , limit: 10
            , curr: curr
            , jump: function (obj, first) {
                if (!first) {
                    getData(obj.curr);

                }
            }
        });
    })
}

//排序
function kindChange(num) {
    //互换
    // var kind_text = $(".kind_sel").select2('val');
    var group1 = $("#kind_sel1").select2('val');
    var group2 = $("#kind_sel2").select2('val');

    $('.order').click(function () {
        var kindLabel = $(this).attr('label');
        $.ajax({
            type: "get",
            url: "https://www.hgxrshn.com/MgProductSorted",
            async: true,
            data: {
                Id: num,
                Kind: kindLabel,
                ProductGroup1: group1,
                ProductGroup2: group2,
            },
            success: function (data) {
                if (data == 'success') {
                    layer.msg('排序成功', {time: 500}, function () {
                        location.reload();
                    })
                } else {
                    layer.msg('排序失败', {time: 500})
                }
            }
        });
    })

    $('.order5').click(function () {
        var firseCheck, secondCheck;
        var check = $('input[type=checkbox]:checked');
        if (check.length == 2) {
            firseCheck = check.eq(0).attr('index');
            secondCheck = check.eq(1).attr('index');
            $.ajax({
                type: "get",
                url: "https://www.hgxrshn.com/MgProductSwap",
                async: true,
                data: {
                    Content: kind_text,
                    Behind: secondCheck,
                    First: firseCheck,

                },
                success: function (data) {
                    if (data == 'success') {
                        layer.msg('交换成功', {time: 500}, function () {
                            location.reload();
                        })
                    } else {
                        layer.msg('交换失败', {time: 500})
                    }

                }
            });
        } else {
            layer.msg('请选中两个商品')
        }
    })


}

function TableView(data) {
    $('.table_data').html('');
    for (var i = 0; i < data.length; i++) {

        var trText = `<tr>
		      		    		<!--<td><input  index="${data[i][0]}" type="checkbox"/></input></td>-->
		      		    		<td>${data[i][0]}</td>
		      		    		<td>${data[i][1]}</td>
		      		    		<td>
		      		    			<img src="${data[i][2]}" height="50"/>
		      		    		</td>
		      		    		<td>
		      		    			${data[i][3]}
		      		    		</td>
		      		    		<td>
		      		    			￥${data[i][4]}
		      		    		</td>
		      		    		
		      		    		<td index="${data[i][0]}" style="width: 120px;">
		      		    			<div class="btn_done">
		      		    				<a class="look_detail" href="#">查看</a>
		      		    				<!--<a class="order_by" href="#">排序</a>-->
		      		    			</div>
		      		    			<div>
		      		    				<a class="kind_del" href="#">此分类下删除商品</a>
		      		    			</div>
		      		    		</td>
		      		    	</tr>`;

        $('.table_data').append(trText);

    }
    data_edtil();

}

function data_edtil() {
    $("#check_all").click(function () {
        if ($(this).prop('checked') == true) {

            $("tbody").find('input[type=checkbox]').prop('checked', 'checked')
        } else {
            $("tbody").find('input[type=checkbox]').prop('checked', false)
        }
    })
    $('.order_by').click(function (ev) {
        var Number = $(this).parent().parent().attr('index');
        var ev = ev || event;
        $('.check_do').css('display', 'block')
        $('.check_do').css('left', ev.clientX + 'px');
        $('.check_do').css('top', ev.clientY + 'px');

        kindChange(Number);
    })
    $('.look_detail').click(function () {
        var Index = $(this).parent().parent().attr('index');
        window.location.href = "http://www.puer.zone:9000/product_details.html?Index=" + Index;
    })
    $('.check_do').mouseleave(function () {
        $('.check_do').css('display', 'none');
    })
    $('.kind_del').click(function () {
        // var kind_text = $(".kind_sel").select2('val');
        var group1 = $("#kind_sel1").select2('val');
        var group2 = $("#kind_sel2").select2('val');
        var id = $(this).parent().parent().attr('index');
        // var checkNode = $("tbody").find('input[type=checkbox]:checked');
        // var checkArr=[];
        // var eventid = $(this).parent().parent().attr('index');
        // if(checkNode.length>0){
        // 	for (var i = 0;i<checkNode.length;i++) {
        // 		var eventId = checkNode.eq(i).attr('index');
        // 		checkArr.push(eventId);
        // 	}
        // }else{
        // 	checkArr.push(eventid);
        // }
        $.ajax({
            type: "get",
            url: "http://www.puer.zone:30000/DeleteSonProduct",
            async: true,
            data: {
                ProductId: id,
                ProductGroup1: group1,
                ProductGroup2: group2
            },
            success: function (data) {
                if (data == 'success') {
                    layer.msg('删除成功', {time: 500}, function () {
                        location.reload()
                    })
                } else {
                    layer.msg('删除失败', {time: 500})
                }
            }
        });
    })
}

function group1Change() {
    var man = ['套西', '单西', '裤子', '长袖衬衫', '短袖衬衫', '马甲', '大衣', '风衣', '夹克', '中华立领', '中式'];
    var child = ['套西', '单西', '裤子', '长袖衬衫', '短袖衬衫', '马甲', '大衣', '风衣'];
    var work = ['男士西装', '男士衬衫', '男士裤子', '男士大衣', '男士夹克', '女士西服', '女士衬衣', '女士短裙', '大衣', '风衣'];
    var group2 = [];
    var group1 = $("#kind_sel1").val();
    if ((group1 == "男装") || (group1 == "女装")) {
        group2 = man;
    } else if (group1 == "童装") {
        group2 = child;
    } else if (group1 == "职业装") {
        group2 = work;
    }

    $("#kind_sel2").empty();
    // var text = `<option value='全部'>全部</option>`;
    // $("#group2").append(text);
    for (var i = 0; i < group2.length; i++) {
        var text = `<option value='${group2[i]}'>${group2[i]}</option>`;
        $("#kind_sel2").append(text);
    }
}